import indexApi from '@/api/index'
import homeApi from '@/api/home'
import { http } from '@/common/axios'
// import { http } from "@/static/js/request.js";
// import { GetAllAreaLonLat } from "@/api/monitor.js";
// import { GetAllOceanMeteoSites } from "@/api/more.js";
// import geojson from "@/static/county.json";
import legendJson from '@/common/legendJson.js'
export default {
	namespaced: false,
	state: {
		qz_citys: null,
		legends: [],
		bounds: null,
		areaLonLat: [],
		siteLonLat: [],
		dzdGeoJson: null,
		stateData: {}
	},
	mutations: {
		setCounty(state, data) {
			state.qz_citys = data
		},
		setLegends(state, data) {
			state.legends = data
		},
		setBounds(state, data) {
			state.bounds = data
		},
		setAreaLonLat(state, data) {
			state.areaLonLat = data
		},
		setSiteLonLat(state, data) {
			state.siteLonLat = data
		},
		setDzSite(state, data) {
			state.dzdGeoJson = data
		},
		// 设置缓存
		setDataByKey(state, data) {
			state.stateData[data.key] = data.data
		}
	},
	actions: {
		// 获取泉州县市
		async getCounty() {
			const resp = await homeApi.getBizOrgTree()
			let cityJson = resp.data[0].children[0].children[0]
			let _list = []
			_list = cityJson.children.map((item) => {
				return {
					areaName: item.name,
					areaCode: item.code,
					children: item.children
				}
			})
			_list.unshift({
				areaName: cityJson.name,
				areaCode: cityJson.code,
				children: cityJson.children
			})
			uni.setStorage({
				key: 'countyInfos',
				data: _list
			})
			return _list
		},
		async getLegends({ commit, state }) {
			if (!state.legends || state.legends.length == 0) {
				// const resp = await indexApi.GetLegendList()
				const resp = legendJson
				// commit('setLegends', resp)
				uni.setStorage({
					key: 'legend',
					data: resp
				})
				return resp
			} else {
				return state.legends
			}
		},
		// 获取边界
		async getBounds({ commit, state }) {
			if (!state.bounds) {
				let params = {
					service: 'WFS',
					request: 'GetFeature',
					version: '1.0.0',
					outputFormat: 'application/json',
					maxFeatures: '10000',
					typeName: 'sanming:xian'
				}
				const resp = await indexApi.geoserver(params)
				// console.log(`geojson:`, geojson);
				// const resp = await http.get('/files/json/qz_county.json')
				commit('setBounds', resp)
				return resp
			} else {
				return state.bounds
			}
		},
		async getAreaLonLat({ commit, state }) {
			if (state.areaLonLat.length == 0) {
				// console.log(`geojson:`, geojson);
				const resp = await GetAllAreaLonLat()
				commit('setAreaLonLat', resp.data)
				return resp.data
			} else {
				return state.areaLonLat
			}
		},
		async getSiteLonLat({ commit, state }) {
			if (state.siteLonLat.length == 0) {
				// console.log(`geojson:`, geojson);
				const resp = await GetAllOceanMeteoSites()
				commit('setSiteLonLat', resp.data)
				return resp.data
			} else {
				return state.siteLonLat
			}
		},
		async getDzSite({ commit, state }, data) {
			if (!state.dzdGeoJson) {
				commit('setDzSite', data)
				return data
			} else {
				return state.dzdGeoJson
			}
		},

		getDataByKey({ commit, state }, data) {
			if (!data || !data.key) {
				return null
			}
			if (!state.stateData[data.key]) {
				commit('setDataByKey', data)
				return data.data
			} else {
				return state.stateData[data.key]
			}
		}
	}
}
